package com.kgc.controller;

import com.kgc.pojo.User;
import com.kgc.pojo.UserDetail;
import com.kgc.service.GetUserInfo_wzlService;
import com.kgc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
/**
 * 获得用户的全部信息(除了密码)
 * @author  王臻利
 */
@RestController
public class GetUserDetailInfo_wzlController {

    @Autowired
    private GetUserInfo_wzlService getUserInfo_wzlService;

    /*获取用户信息*/
    /*缓存注解的使用查询用Cacheable,增删改用ChacheEvict*/
    @Cacheable(value="user",key = "'getUserdetailInfo_wzl'")
    @GetMapping("getUserdetailInfo_wzl")
    public Object getUserdetailInfo_wzl(){
        System.out.println("获取了用户的信息");
        List<User> user = getUserInfo_wzlService.getUserdetailInfo_wzl();
        return  new Result(user,"获取用户信息成功");
    }
    /*删除用户信息*/
    @CacheEvict(value="user",key = "'getUserdetailInfo_wzl'")
    @GetMapping("delUserinfo_wzl")
    public Object delUserinfo_wzl(int id){
        /*删除用户列表*/
        int count = getUserInfo_wzlService.delUserById_wzl(id);
        /*删除用户详情表*/
        int count2 =getUserInfo_wzlService.delDetailById_wzl(id);
        return new Result(count,"删除用户成功");
    }
    /*增加用户信息*/
    @PostMapping("addUserInfo_wzl")
    public Object addUserInfo(User user){
        List<User> user2 = getUserInfo_wzlService.getUserdetailInfo_wzl();
        /*循环遍历判断数据库中是否有相同的用户*/
        for (User user1:user2) {
            if (user1.getPhone().equals(user.getPhone())) {
                return new Result(false, "该用户已存在请从新输入");
            }
        }
        int count = getUserInfo_wzlService.addUser_wzl(user);
        return new Result(true,"用户增加成功");
    }
    /*增加用户详细信息*/
    @PostMapping("addDetailUserInfo_wzl")
    public Object addDetailUserInfo(UserDetail userDetail){

        int count =getUserInfo_wzlService.addUser_wzl(userDetail);
        return new Result(count,"用户详情增加成功");
    }
    /*查询最后用户的信息用户信息*/
    @PostMapping("selectUserLastInfo_wzl")
    public Object selectUserLastInfo_wzl(){
        User user =getUserInfo_wzlService.selectUserLastInfo_wzl();
        Integer num = user.getId();
        return new Result(num,"用户查询成功");
    }
}
